import QueryList from '@/components/basicWidget/QueryList/QueryListOld'
import { getContext } from '@/context/common/applicationContext'
import React, { Component } from 'react'
import paramUtil from '@/utils/paramUtil'
import { Tag, Spin } from 'antd'
import { Link } from 'react-router-dom'

class CrawlPersonal extends Component {

    constructor(props) {
        super(props)
        this.state = {
            title: '我的任务',
            commandCode: 'getTaskInfoOfCurrentUser',
            scheme: {
                buttons: [
                    { type: 'button', onClick: 'onQuery', title: '查询' },
                    { type: 'button', onClick: 'reSetStatus', title: '重置' }
                ],
                properties: [
                    { type: 'InputWidget', description: '爬取关键词', name: 'importantCode' },
                    { type: 'RadioWidget', description: '服务状态', name: 'taskStatus', paramCode: 'crawlTaskStatus' },
                    { type: 'RadioWidget', description: '服务类型', name: 'taskType', paramCode: 'crawlTaskType' },
                ]
            },
            loading: true
        }
    }

    async componentDidMount() {
        //获取参数
        let crawlTaskStatus = await paramUtil.get('crawlTaskStatus')
        let crawlTaskType = await paramUtil.get('crawlTaskType')

        await new Promise(resolve => {
            this.setState({
                crawlTaskStatus,
                crawlTaskType,
                loading: false
            }, resolve)
        })
    }


    render() {
        if (this.state.loading === true) return <Spin />
        const listConfig = {
            columns: [
                { title: '服务类型', dataIndex: 'taskTypeDisplay' },
                { title: '服务名称', dataIndex: 'taskName' },
                { title: '爬取关键词', dataIndex: 'importantCode' },
                { title: '创建时间', dataIndex: 'createTime', sorter: (a, b) => a.createTime > b.createTime ? 1 : -1 },
                { title: '最后修改时间', dataIndex: 'alterTime', sorter: (a, b) => a.alterTime > b.alterTime ? 1 : -1 },
                { title: '服务状态', dataIndex: 'taskStatusDisplay' }
            ],
            bordered: true,
            showTotal: true,
            pageSizeOptions: [10, 20, 50, 100],
            gotoPage: true,
            actionColumns: (columns, data) => {
                data = data.map(item => {
                    item.taskName = (
                        <Link to={`/jms/crawl/${item.taskType}?taskName=${item.taskName}`}>{item.taskName}</Link>
                    )
                    const type = (this.state.crawlTaskType.children || []).find(item2 => item2.code === item.taskType)
                    item.taskTypeDisplay = type ? type.content : item.taskType
                    item.importantCode = JSON.parse(item.importantCode).join(',')
                    let status = null
                    switch (item.taskStatus) {
                        case 'RUNNING':
                            status = <Tag color='geekblue'>运行中</Tag>
                            break
                        case 'WAITING':
                            status = <Tag color='volcano'>等待中</Tag>
                            break
                        case 'END':
                            status = <Tag color='success'>已完成</Tag>
                            break
                        default:
                            status = item.taskStatus
                    }
                    item.taskStatusDisplay = status
                    return item
                })
                return { newColumns: columns, newData: data }
            }
        }

        return (
            <QueryList
                title={this.state.title}
                commandCode={this.state.commandCode}
                scheme={this.state.scheme}
                listConfig={listConfig}
                kvMap={{custId: this.props.user ? this.props.user.id : ''}}
                ref={queryList => this.queryList = queryList}
            >

            </QueryList>
        )
    }
}

export default getContext(CrawlPersonal)
